package com.xiyi.shouxin.controller;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import com.xiyi.shouxin.utils.SendMsgUtils;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.google.gson.Gson;
import com.xiyi.shouxin.controller.pojo.ListUserParams;
import com.xiyi.shouxin.controller.pojo.ListUserPiciParams;
import com.xiyi.shouxin.controller.pojo.RankParams;
import com.xiyi.shouxin.controller.pojo.SmsParams;
import com.xiyi.shouxin.controller.pojo.WXLoginParams;
import com.xiyi.shouxin.controller.pojo.RegisterParams;
import com.xiyi.shouxin.entity.admin.Admin;
import com.xiyi.shouxin.entity.user.User;
import com.xiyi.shouxin.exception.ErrorCode;
import com.xiyi.shouxin.init.CacheUtil;
import com.xiyi.shouxin.init.DelayTask;
import com.xiyi.shouxin.service.PracticeService;
import com.xiyi.shouxin.service.UserService;
import com.xiyi.shouxin.utils.ExceptionUtil;
import com.xiyi.shouxin.utils.PhoneNumberUtil;
import com.xiyi.shouxin.utils.WXUtil;
import com.xiyi.shouxin.utils.WXUtil.Code2SessionResponse;
import com.xiyi.shouxin.view.CommonModel;
import com.xiyi.shouxin.view.pojo.ItemPage;
import com.xiyi.shouxin.view.pojo.UserPracticeTimeView;
import com.xiyi.shouxin.view.pojo.UserView;
import com.xiyi.shouxin.entity.user.Practice;
import com.xiyi.shouxin.entity.user.Cpr;
import com.xiyi.shouxin.global.PracticeDataType;
import com.xiyi.shouxin.global.QualifiedEnum;

import java.math.BigDecimal;
import java.util.Date;
import org.springframework.http.MediaType;
import javax.servlet.http.HttpServletRequest;

@CrossOrigin("*")
@RequestMapping("/user")
@RestController
public class UserController {
    private Gson gson = new Gson();
    @Autowired
    private Logger logger;
    @Autowired
    private CacheUtil cacheUtil;
    @Autowired
    private WXUtil wxUtil;
    @Autowired
    private UserService userService;
    @Autowired
    private PracticeService practiceService;

    @PostMapping("/list_user")
    public CommonModel listUser(@RequestBody ListUserParams params) {
        if (params == null || params.getPage() == null || params.getSize() == null) {
            return CommonModel.Fail(ErrorCode.INVALIDPARAMTER.getCode(), ErrorCode.INVALIDPARAMTER.getMsg(), null);
        }
        Long adminId = null;
        Admin admin = cacheUtil.getAdminOperator();
        if (admin != null) {
            adminId = admin.getId();
        }
        try {
            ItemPage<UserView> info = userService.findUserList(adminId, params.getBindId(), params.getKeyword(),
                    params.getStartTime(),
                    params.getEndTime(),
                    params.getPage(),
                    params.getSize());
            return CommonModel.Success(info);
        } catch (Exception e) {
            logger.error("{}查询用户列表{}异常：", gson.toJson(cacheUtil.getAdminOperator()), gson.toJson(params), e);
            return ExceptionUtil.GetCommonModel(e);
        }
    }

    @PostMapping("/list_user_pici")
    public CommonModel listUserPici(@RequestBody ListUserPiciParams params) {
        if (params == null || params.getPage() == null || params.getSize() == null) {
            return CommonModel.Fail(ErrorCode.INVALIDPARAMTER.getCode(), ErrorCode.INVALIDPARAMTER.getMsg(), null);
        }

        try {
            ItemPage<UserView> info = userService.findUserPracticeList(params.getBindId(), params.getPiciId(),
                    params.getPage(), params.getSize());
            return CommonModel.Success(info);
        } catch (Exception e) {
            logger.error("{}查询用户批次列表{}异常：", gson.toJson(cacheUtil.getAdminOperator()), gson.toJson(params), e);
            return ExceptionUtil.GetCommonModel(e);
        }
    }

    /**
     * 通过短信验证码登录
     * @param params
     * @return
     */
    @PostMapping("/loginBySmsCode")
    public CommonModel LoginBySmsCode(@RequestBody SmsParams params) {
        if (params == null || !StringUtils.hasText(params.getCode())) {
            return CommonModel.Fail(ErrorCode.INVALIDPARAMTER.getCode(), "code无效", null);
        }
        try {
            String phoneCode = cacheUtil.getPhoneCode(params.getPhone());
            //验证满一直
            if(!phoneCode.equals(params.getCode())){
                return CommonModel.Fail(ErrorCode.INTERNAL.getCode(), "验证码错误", null);

            }



            User view = userService.findUserByPhone(params.getPhone());
            String token = userService.generateTokenForUser(view);
            cacheUtil.putTokenUser(token, view);
            cacheUtil.offerDelayTask(new DelayTask(token, 30L * 24 * 3600 * 1000));
            Map<String, Object> data = new HashMap<>();
            data.put("token", token);
            data.put("info", view);
            return CommonModel.Success(data);
        } catch (Exception e) {
            logger.error("微信登录{}异常：", gson.toJson(params), e);
            return ExceptionUtil.GetCommonModel(e);
        }
    }

    @PostMapping("/wx_code_login")
    public CommonModel wxCodeLogin(@RequestBody WXLoginParams params) {
        if (params == null || !StringUtils.hasText(params.getWxCode())) {
            return CommonModel.Fail(ErrorCode.INVALIDPARAMTER.getCode(), "code无效", null);
        }
        try {
            Code2SessionResponse res = wxUtil.Code2Session(params.getWxCode());
            if (res.errcode != 0) {
                return CommonModel.Fail(ErrorCode.INTERNAL.getCode(), "微信登录异常", null);
            }
            UserView view = userService.findUserByOpenid(res.openid);
            if (view == null) {
                view = userService.createUser(res.openid);
            }
            String token = userService.generateTokenForUser(view);
            cacheUtil.putTokenUser(token, view);
            cacheUtil.offerDelayTask(new DelayTask(token, 30L * 24 * 3600 * 1000));
            Map<String, Object> data = new HashMap<>();
            data.put("token", token);
            data.put("info", view);
            return CommonModel.Success(data);
        } catch (Exception e) {
            logger.error("微信登录{}异常：", gson.toJson(params), e);
            return ExceptionUtil.GetCommonModel(e);
        }
    }

    /**
     * 发送验证码
     * @param params
     * @return
     */

    @PostMapping("/sendSms")
    public CommonModel sendSms(@RequestBody SmsParams params) {
        if (params == null || !StringUtils.hasText(params.getPhone())) {
            return CommonModel.Fail(ErrorCode.INVALIDPARAMTER.getCode(), "手机号格式错误", null);
        }
        if (!PhoneNumberUtil.isValidChineseMobileNumber(params.getPhone())) {
            return CommonModel.Fail(ErrorCode.INVALIDPARAMTER.getCode(), "手机号格式错误", null);
        }

        // 新增：校验手机号是否已在系统注册
        try {
            User exist = userService.findUserByPhone(params.getPhone());
            if (exist == null) {
                return CommonModel.Fail(ErrorCode.INVALIDPARAMTER.getCode(), "手机号未注册，无法发送验证码", null);
            }
        } catch (Exception e) {
            logger.error("校验手机号是否注册异常：{}", gson.toJson(params), e);
            return ExceptionUtil.GetCommonModel(e);
        }

     /*   if (cacheUtil.getPhoneCode(params.getPhone()) != null) {
            return CommonModel.Fail(ErrorCode.INVALIDPARAMTER.getCode(), "请勿重复发送验证码", null);
        }*/
        //生成随机验证码
        Random random = new Random();
        String code = String.format("%06d", random.nextInt(1000000));
        try {
            cacheUtil.putPhoneCode(params.getPhone(), code );
            //过期时间3分钟
            cacheUtil.offerDelayTask(new DelayTask(params.getPhone(), 60 * 1000*30));
            //验证码实际发送操作
            SendMsgUtils.sendSms(params.getPhone(), code);

            return CommonModel.Success(null);

        } catch (Exception e) {
            logger.error("发送短信{}异常：", gson.toJson(params), e);
            return ExceptionUtil.GetCommonModel(e);
        }
    }

    @PostMapping(value = "/sendSms", consumes = MediaType.TEXT_PLAIN_VALUE)
    public CommonModel sendSmsText(@RequestBody String body) {
        try {
            SmsParams params = gson.fromJson(body, SmsParams.class);
            return sendSms(params);
        } catch (Exception ex) {
            logger.error("解析短信请求文本失败：{}", body, ex);
            return CommonModel.Fail(ErrorCode.INVALIDPARAMTER.getCode(), "请求体解析失败，应为JSON格式", null);
        }
    }

    @PostMapping("/get_user_data")
    public CommonModel getUserData() {
        User user = cacheUtil.getUserOperator();
        if (user == null) {
            return CommonModel.Fail(ErrorCode.INVALIDTOKEN.getCode(), "请重新登录", null);
        }
        try {
            UserView view = userService.findUser(user.getId());
            return CommonModel.Success(view);
        } catch (Exception e) {
            logger.error("获取用户信息异常：", e);
            return ExceptionUtil.GetCommonModel(e);
        }
    }

    /**
     * 用户详情接口
     */
    @PostMapping("/get_user_detail")
    public CommonModel getUserDetail() {
        User user = cacheUtil.getUserOperator();
        Admin admin = cacheUtil.getAdminOperator();
        Long userId = null;
        if (user != null) {
            userId = user.getId();
        } else if (admin != null) {
            try {
                User userByAdmin = userService.findUserByAdminId(admin.getId());
                if (userByAdmin != null) {
                    userId = userByAdmin.getId();
                }
            } catch (Exception e) {
                logger.error("通过adminId查找用户异常", e);
            }
        }
        if (userId == null) {
            return CommonModel.Fail(ErrorCode.INVALIDTOKEN.getCode(), "请重新登录", null);
        }
        try {
            // 直接返回User对象，字段与user表结构一致
            User userEntity = userService.getUserEntity(userId);
            return CommonModel.Success(userEntity);
        } catch (Exception e) {
            logger.error("获取用户详情异常：", e);
            return ExceptionUtil.GetCommonModel(e);
        }
    }

    @PostMapping("/update_user")
    public CommonModel updateUser(@RequestBody User params) {
        if (params == null || params.getId() == null) {
            return CommonModel.Fail(ErrorCode.INVALIDPARAMTER.getCode(), ErrorCode.INVALIDPARAMTER.getMsg(), null);
        }
        try {
            userService.updateUser(params);
            return CommonModel.Success(null);
        } catch (Exception e) {
            logger.error("{}修改用户{}异常：", gson.toJson(cacheUtil.getAdminOperator()), gson.toJson(params), e);
            return ExceptionUtil.GetCommonModel(e);
        }
    }

    @PostMapping("/perfect_user_data")
    public CommonModel perfectUserData(@RequestBody User params) {
        User user = cacheUtil.getUserOperator();
        if (user == null) {
            return CommonModel.Fail(ErrorCode.INVALIDTOKEN.getCode(), "请重新登录", null);
        }
        params.setId(user.getId());
        try {
            userService.updateUser(params);
            return CommonModel.Success(null);
        } catch (Exception e) {
            logger.error("完善用户信息{}异常：", gson.toJson(params), e);
            return ExceptionUtil.GetCommonModel(e);
        }
    }

    @PostMapping("/unbind_user")
    public CommonModel unbindUser(@RequestBody User params) {
        User user = cacheUtil.getUserOperator();
        try {
            if (user != null) {
                userService.unbindUser(user.getId());
            } else if (params != null && params.getId() != null) {
                userService.unbindUser(params.getId());
            }
            return CommonModel.Success(null);
        } catch (Exception e) {
            logger.error("解绑用户{}异常：", gson.toJson(params), e);
            return ExceptionUtil.GetCommonModel(e);
        }
    }

    @PostMapping("/bindAdmin")
    public CommonModel bindAdmin(@RequestBody User params) {
        if (params == null || params.getAdminId() == null) {
            return CommonModel.Fail(ErrorCode.INVALIDPARAMTER.getCode(), ErrorCode.INVALIDPARAMTER.getMsg(), null);
        }
        User user = cacheUtil.getUserOperator();
        if (user == null) {
            return CommonModel.Fail(ErrorCode.INVALIDTOKEN.getCode(), "请重新登录", null);
        }
        try {
            userService.bindUser(user.getId(), params.getAdminId());
            return CommonModel.Success(null);
        } catch (Exception e) {
            logger.error("绑定用户{}异常：", gson.toJson(params), e);
            return ExceptionUtil.GetCommonModel(e);
        }
    }

    @PostMapping("/bind_phone")
    public CommonModel bindPhone(@RequestBody SmsParams params) {
        if (params == null || params.getPhone() == null || params.getCode() == null) {
            return CommonModel.Fail(ErrorCode.INVALIDPARAMTER.getCode(), ErrorCode.INVALIDPARAMTER.getMsg(), null);
        }
        User user = cacheUtil.getUserOperator();
        if (user == null) {
            return CommonModel.Fail(ErrorCode.INVALIDTOKEN.getCode(), "请重新登录", null);
        }
        try {
            String code = cacheUtil.getPhoneCode(params.getPhone());
            if (code == null || !code.equals(params.getCode())) {
                return CommonModel.Fail(ErrorCode.INVALIDCODE.getCode(), "验证码错误", null);
            }
            userService.bindUserPhone(user.getId(), params.getPhone());
            return CommonModel.Success(null);
        } catch (Exception e) {
            logger.error("绑定用户{}异常：", gson.toJson(params), e);
            return ExceptionUtil.GetCommonModel(e);
        }
    }

    @PostMapping("/rank")
    public CommonModel rank(@RequestBody RankParams params) {
        if (params == null || params.getType() == null) {
            return CommonModel.Fail(ErrorCode.INVALIDPARAMTER.getCode(), ErrorCode.INVALIDPARAMTER.getMsg(), null);
        }
        User user = cacheUtil.getUserOperator();
        if (user == null) {
            return CommonModel.Fail(ErrorCode.INVALIDTOKEN.getCode(), "请重新登录", null);
        }
        try {
            UserPracticeTimeView view = practiceService.countUserRank(user.getId(), params.getType());
            return CommonModel.Success(view);
        } catch (Exception e) {
            logger.error("查询用户排名{}异常：", gson.toJson(params), e);
            return ExceptionUtil.GetCommonModel(e);
        }
    }

    /**
     * 用户注册
     * @param params
     * @return
     */
    @PostMapping("/register")
    public CommonModel register(@RequestBody RegisterParams params) {
        if (params == null || !StringUtils.hasText(params.getPhone()) ||
            !StringUtils.hasText(params.getPassword()) ||
            !StringUtils.hasText(params.getNickName())) {
            return CommonModel.Fail(ErrorCode.INVALIDPARAMTER.getCode(), "手机号、密码、真实姓名为必填项", null);
        }
        if (!PhoneNumberUtil.isValidChineseMobileNumber(params.getPhone())) {
            return CommonModel.Fail(ErrorCode.INVALIDPARAMTER.getCode(), "手机号格式错误", null);
        }
        try {
            userService.registerUser(params);
            return CommonModel.Success(null);
        } catch (Exception e) {
            logger.error("用户注册异常：", e);
            return ExceptionUtil.GetCommonModel(e);
        }
    }

    /**
     * 修改头像
     * @param params
     * @return
     */
    @PostMapping("/update_avatar")
    public CommonModel updateAvatar(@RequestBody Map<String, String> params) {
        if (params == null || !StringUtils.hasText(params.get("avatar"))) {
            return CommonModel.Fail(ErrorCode.INVALIDPARAMTER.getCode(), "头像地址不能为空", null);
        }
        User user = cacheUtil.getUserOperator();
        Admin admin = cacheUtil.getAdminOperator();

        Long userId = null;
        if (user != null) {
            userId = user.getId();
        } else if (admin != null) {
            // 管理员登录，查找其绑定的 user 记录
            User userByAdmin = null;
            try {
                userByAdmin = userService.findUserByAdminId(admin.getId());
                System.out.println(userByAdmin);
            } catch (Exception e) {
                logger.error("通过adminId查找用户异常", e);
            }
            if (userByAdmin == null) {
                return CommonModel.Fail(ErrorCode.INVALIDPARAMTER.getCode(), "未找到对应的用户信息", null);
            }
            userId = userByAdmin.getId();
        } else {
            return CommonModel.Fail(ErrorCode.INVALIDTOKEN.getCode(), "请重新登录", null);
        }

        try {
            userService.updateUserAvatar(userId, params.get("avatar"));
            Map<String, Object> result = new HashMap<>();
            result.put("message", "头像修改成功");
            result.put("avatar", params.get("avatar"));
            return CommonModel.Success(result);
        } catch (Exception e) {
            logger.error("修改头像异常：", e);
            return ExceptionUtil.GetCommonModel(e);
        }
    }

    /**
     * 修改密码
     * @param params
     * @return
     */
    @PostMapping("/update_password")
    public CommonModel updatePassword(@RequestBody Map<String, String> params) {
        if (params == null || !StringUtils.hasText(params.get("oldPassword")) ||
            !StringUtils.hasText(params.get("newPassword"))) {
            return CommonModel.Fail(ErrorCode.INVALIDPARAMTER.getCode(), "原密码和新密码不能为空", null);
        }
        
        // 优先获取用户信息，如果没有则获取管理员信息
        User user = cacheUtil.getUserOperator();
        Admin admin = cacheUtil.getAdminOperator();
        
        if (user == null && admin == null) {
            return CommonModel.Fail(ErrorCode.INVALIDTOKEN.getCode(), "请重新登录", null);
        }
        
        try {
            if (user != null) {
                // 用户操作
                userService.updateUserPassword(user.getId(), params.get("oldPassword"), params.get("newPassword"));
            } else {
                // 管理员操作
                userService.updateAdminPassword(admin.getId(), params.get("oldPassword"), params.get("newPassword"));
            }
            
            Map<String, Object> result = new HashMap<>();
            result.put("message", "密码修改成功");
            result.put("updateTime", new java.util.Date());
            return CommonModel.Success(result);
        } catch (Exception e) {
            logger.error("修改密码异常：", e);
            return ExceptionUtil.GetCommonModel(e);
        }
    }

    /**
     * 保存练习数据接口
     * 与 WebSocket 逻辑保持一致，用于保存 CPR 练习数据
     */
    @PostMapping("/save_practice_data")
    public CommonModel savePracticeData(@RequestBody Map<String, Object> params) {
        if (params == null) {
            return CommonModel.Fail(ErrorCode.INVALIDPARAMTER.getCode(), "参数不能为空", null);
        }
        
        // 获取当前用户信息
        User user = cacheUtil.getUserOperator();
        Admin admin = cacheUtil.getAdminOperator();
        Long userId = null;
        
        if (user != null) {
            userId = user.getId();
        } else if (admin != null) {
            try {
                User userByAdmin = userService.findUserByAdminId(admin.getId());
                if (userByAdmin != null) {
                    userId = userByAdmin.getId();
                }
            } catch (Exception e) {
                logger.error("通过adminId查找用户异常", e);
            }
        }
        
        if (userId == null) {
            return CommonModel.Fail(ErrorCode.INVALIDTOKEN.getCode(), "请重新登录", null);
        }
        
        try {
            // 解析参数
            Integer type = (Integer) params.get("type");
            String data = (String) params.get("data");
            Long practiceId = params.get("practiceId") != null ? Long.valueOf(params.get("practiceId").toString()) : null;
            Long piciId = params.get("piciId") != null ? Long.valueOf(params.get("piciId").toString()) : null;
            
            if (type == null) {
                return CommonModel.Fail(ErrorCode.INVALIDPARAMTER.getCode(), "练习类型不能为空", null);
            }
            
            // 根据练习类型处理数据
            if (type == PracticeDataType.JINGXINGZHONG) {
                // 练习进行中 - 保存 CPR 数据
                if (!StringUtils.hasText(data)) {
                    return CommonModel.Fail(ErrorCode.INVALIDPARAMTER.getCode(), "练习数据不能为空", null);
                }
                
                if (practiceId == null) {
                    return CommonModel.Fail(ErrorCode.INVALIDPARAMTER.getCode(), "练习ID不能为空", null);
                }
                
                // 解析16进制数据：时间(2-6位)和深度(8-12位)
                Integer time = Integer.parseInt(data.substring(2, 6), 16);
                Integer depth = Integer.parseInt(data.substring(8, 12), 16);
                
                // 创建CPR练习数据记录
                Cpr cpr = new Cpr();
                cpr.setUid(userId);
                
                // 深度数据转换：从整数转换为小数（除以1000）
                BigDecimal value = new BigDecimal(depth);
                value = value.divide(new BigDecimal(1000));
                BigDecimal roundedValue = value.setScale(2, java.math.RoundingMode.HALF_UP);
                cpr.setDepth(roundedValue.doubleValue());
                
                // 计算练习质量
                cpr.setQualified(practiceService.calQualified(cpr.getDepth(), time).getQualified());
                cpr.setPracticeId(practiceId);
                cpr.setTime(time);
                cpr.setMillisecond(System.currentTimeMillis());
                cpr.setCreatedAt(new Date().toString());
                
                // 保存练习数据
                practiceService.practiceData(cpr);
                
                Map<String, Object> result = new HashMap<>();
                result.put("message", "练习数据保存成功");
                result.put("depth", cpr.getDepth());
                result.put("time", cpr.getTime());
                result.put("qualified", cpr.getQualified());
                return CommonModel.Success(result);
                
            } else if (type == PracticeDataType.KAISHIFEIKAOHE) {
                // 开始非考核练习
                Long adminId = null;
                if (user != null) {
                    adminId = user.getAdminId();
                }
                
                // 结束用户之前的练习（如果有）
                if (practiceId != null) {
                    practiceService.endPractice(userId, practiceId);
                }
                
                // 开始新的非考核练习
                Practice practice = practiceService.startPractice(userId, adminId, piciId);
                
                Map<String, Object> result = new HashMap<>();
                result.put("message", "非考核练习开始成功");
                result.put("practiceId", practice.getId());
                return CommonModel.Success(result);
                
            } else if (type == PracticeDataType.KAISHIKAOHE) {
                // 开始考核练习
                Long adminId = null;
                if (user != null) {
                    adminId = user.getAdminId();
                }
                
                // 结束用户之前的练习（如果有）
                if (practiceId != null) {
                    practiceService.endPractice(userId, practiceId);
                }
                
                // 开始新的考核练习
                Practice practice = practiceService.startCheckPractice(userId, adminId, piciId);
                
                Map<String, Object> result = new HashMap<>();
                result.put("message", "考核练习开始成功");
                result.put("practiceId", practice.getId());
                return CommonModel.Success(result);
                
            } else if (type == PracticeDataType.JIESHU) {
                // 结束练习
                if (practiceId == null) {
                    return CommonModel.Fail(ErrorCode.INVALIDPARAMTER.getCode(), "练习ID不能为空", null);
                }
                
                practiceService.endPractice(userId, practiceId);
                
                Map<String, Object> result = new HashMap<>();
                result.put("message", "练习结束成功");
                return CommonModel.Success(result);
                
            } else if (type == PracticeDataType.HUITANBUZU) {
                // 回弹不足
                if (practiceId == null) {
                    return CommonModel.Fail(ErrorCode.INVALIDPARAMTER.getCode(), "练习ID不能为空", null);
                }
                
                practiceService.notrestPractice(practiceId);
                
                Map<String, Object> result = new HashMap<>();
                result.put("message", "回弹不足记录成功");
                return CommonModel.Success(result);
                
            } else {
                return CommonModel.Fail(ErrorCode.INVALIDPARAMTER.getCode(), "不支持的练习类型", null);
            }
            
        } catch (Exception e) {
            logger.error("保存练习数据异常：", e);
            return ExceptionUtil.GetCommonModel(e);
        }
    }

    /**
     * 退出登录：移除当前 Authorization 对应的会话
     */



    @PostMapping("/logout")
    public CommonModel logout(HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        if (!StringUtils.hasText(token)) {
            return CommonModel.Fail(ErrorCode.INVALIDPARAMTER.getCode(), "缺少Authorization", null);
        }
        try {
            cacheUtil.removeTokenUser(token);
            cacheUtil.removeTokenAdmin(token);
            // 可选：同时移除可能以token为键的延时项/验证码
            cacheUtil.removePhoneCode(token);
            return CommonModel.Success(new HashMap<String, Object>() {{ put("message", "已退出登录"); }});
        } catch (Exception e) {
            logger.error("退出登录异常：token={}", token, e);
            return ExceptionUtil.GetCommonModel(e);
        }
    }

}
